package com.shenglue.ssi.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shenglue.ssi.domain.Warehouse;
import com.shenglue.ssi.service.WarehouseService;

public class WarehouseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private Warehouse warehouse;     // 对应的 Bean
	private int warehouseId;    // 删除/修改 的 记录之Id
	private WarehouseService warehouseService;  // 依赖的 Service

	public WarehouseService getWarehouseService() {
		return warehouseService;
	}

	public void setWarehouseService(WarehouseService warehouseService) {
		this.warehouseService = warehouseService;
	}

	public Warehouse getWarehouse() {
		return warehouse;
	}

	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	public int getWarehouseId() {
		return warehouseId;
	}

	public void setWarehouseId(int warehouseId) {
		this.warehouseId = warehouseId;
	}

	public WarehouseAction() {
	}


	/**
	 * 查询所有经销商信息方法
	 */
	public String queryAll() throws Exception {
		List<Warehouse> list = warehouseService.selectAllWarehouse();
		if(list!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "warehouseList", list );
		}else{
			// 不能认为是错误，因为一开始是没有数据的  return INPUT;
		}
		return SUCCESS;
	}

	/**
	 * 增加经销商记录方法
	 */
	public String addNew() throws Exception {
		boolean flag = warehouseService.saveWarehouse( warehouse );
		if(flag==true){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 删除经销商记录方法
	 */
	public String delete() throws Exception {

		// 获取request对象
		boolean flag = warehouseService.deleteWarehouse( warehouseId );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 修改经销商记录方法
	 */
	public String update() throws Exception {

		boolean flag = warehouseService.updateWarehouse( warehouse );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
}
